package com.jd.myaaaccount;

import com.jd.myaaaccount.entity.User;
import com.jd.myaaaccount.model.Common;
import com.jd.myaaaccount.model.UserModel;
import com.jd.myaaaccount.util.AlertDialogHelper;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.MD5Helper;
import com.jd.myaaaccount.util.OperateCode;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegistActivity extends BaseActivity {
	private EditText etName;
	private EditText etEmail;
	private EditText etPassword;
	private EditText etPhone;
	private Button btnRegister;

	private Handler handler;
	private UserModel pm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		iniControl();
		pm = new UserModel(this);
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case OperateCode.REGIST:// 注册结果
					pd.cancel();
					if (msg.arg1 == ErrorCode.SUCCESS) {// 注册成功
						new AlertDialog.Builder(RegistActivity.this)
								.setTitle("提示")
								.setMessage("注册成功")
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {
											@Override
											public void onClick(
													DialogInterface arg0,
													int arg1) {
												finish();
											};
										}).show();
					} else {
						Common.showErrorInfo(RegistActivity.this, msg.arg1,
								"注册失败", 0);
					}
					break;
				default:
					break;
				}
			};
		};
		btnRegister.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				// new Thread(new Runnable() {
				//
				// @Override
				// public void run() {
				// // TODO Auto-generated method stub
				// User user = new User();
				// user.setName("jd");
				// user.setEmail("kjxjx@163.com");
				// user.setPassword(MD5Helper.md5("123456"));
				// user.setPhone("12345678901");
				//
				// pm.regist(user, handler);
				//
				// }
				// }).start();
				register();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

	private void iniControl() {
		etName = (EditText) findViewById(R.id.etName);
		etEmail = (EditText) findViewById(R.id.etEmail);
		etPassword = (EditText) findViewById(R.id.etPassword);

		btnRegister = (Button) findViewById(R.id.btnRegister);

		// 测试初始化数据
		etName.setText("蒋东");
		etEmail.setText("554334484@qq.com");
		etPassword.setText("888888");
		etPhone.setText("13498769576");
	}

	private void register() {
		String name = etName.getText().toString();
		if (name.equals("")) {
			Toast.makeText(RegistActivity.this, "用户名不能为空", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		String email = etEmail.getText().toString();
		if (email.equals("")) {
			Toast.makeText(RegistActivity.this, "邮箱不能为空", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		String password = etPassword.getText().toString();
		if (password.equals("")) {
			Toast.makeText(RegistActivity.this, "密码不能为空", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		password = MD5Helper.md5(password);
		String phone = etPhone.getText().toString();
		final User user = new User();
		user.setName(name);
		user.setEmail(email);
		user.setPassword(password);
		user.setPhone(phone);
		pd = AlertDialogHelper.showWaitProgressDialog(RegistActivity.this,
				"注册中…");
		new Thread(new Runnable() {
			@Override
			public void run() {
				pm.regist(user, handler);
			}
		}).start();
	}
}
